use serde::{Deserialize, Serialize};
use wither::{
    bson::{oid::ObjectId, to_bson, DateTime, Document},
    Model,
};

use crate::AppResult;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum TaskTimeTableType {
    Daily,
}

/// ## 一些定时任务的定时表
/// 目前暂时弃用
#[derive(Debug, Clone, Serialize, Deserialize, Model)]
pub struct TaskTimeTableModel {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub execute_at: DateTime,
    #[serde(rename = "type")]
    pub task_type: TaskTimeTableType,
}

impl TaskTimeTableModel {
    pub fn with_type_and_time<T: Into<TaskTimeTableType>, E: Into<DateTime>>(
        task_type: T,
        execute_at: E,
    ) -> Self {
        Self {
            id: None,
            task_type: task_type.into(),
            execute_at: execute_at.into(),
        }
    }
    pub fn get_filter_document(&self) -> AppResult<Document> {
        let mut document = Document::new();
        document.insert("type", to_bson(&self.task_type)?);
        Ok(document)
    }
}
